from Config.myConstant import *
from Config.myConfig import *
from DataPrepare.tickFactorsLongTerm.factorBase import factorBase
from DataAccess.TickDataProcess import TickDataProcess
import pandas as pd
import numpy as np
########################################################################
class targetFactor(factorBase):
    """描述训练目标的函数"""
    #----------------------------------------------------------------------
    def __init__(self):
        #super(buySellVolumeRatio,self).__init__()
        super().__init__()
        self.factor='targetFactor'
        pass
    #----------------------------------------------------------------------
    def getFactorFromLocalFile(self,code,date):
        mydata=super().getFromLocalFile(code,date,'targetFactor')
        return mydata
        pass
    #----------------------------------------------------------------------
    def updateFactor(self,code,date,data=pd.DataFrame()):
        exists=super().checkLocalFile(code,date,self.factor)
        if exists==True:
            logger.info(f'No need to compute! {self.factor} of {code} in {date} exists!')
            pass
        if data.shape[0]==0:
             #data=TickDataProcess().getDataByDateFromLocalFile(code,date)
             data=TickDataProcess().getTickShotDataFromInfluxdbServer(code,date)
        result=self.computerFactor(code,date,data)
        super().updateFactor(code,date,self.factor,result)
    #----------------------------------------------------------------------
    def __computeVwap(self,data,n):
        result=data[['midPrice','amount','volume']].copy()
        colname='vwap'+str(n)+'ticks'
        result[colname]= (result['amount']-result['amount'].shift(n))/(result['volume']-result['volume'].shift(n))
        select=result.index[0:n]
        result.loc[select,colname]=(result['amount']-result['amount'].iloc[0])/(result['volume']-result['volume'].iloc[0])[select]
        result[colname]=result[colname].replace(np.inf,np.nan)
        result[colname]=result[colname].replace(-np.inf,np.nan)
        select=result[colname].isna()==True
        result.loc[select,colname]=result['midPrice'][select]
        #data[colname]=result[colname]
        return result[colname]
        pass
    #----------------------------------------------------------------------
    def computerFactor(self,code,date,mydata):
        result=pd.DataFrame()
        if mydata.shape[0]!=0:
            #index对齐即可
            result=mydata[['midPrice','tick','amount','volume']].copy()
            result['midPrice'].fillna(method='ffill',inplace=True)
            mydata['vwap15s']=self.__computeVwap(result,5)

            #mid价格的增长率 10m
            result['midIncreaseNext10m']=mydata['vwap15s'].shift(-200)/mydata['midPrice']-1
            result['midIncreaseNext1m']=mydata['vwap15s'].shift(-20)/mydata['midPrice']-1
            result['midIncreaseNext2m']=mydata['vwap15s'].shift(-40)/mydata['midPrice']-1
            result['midIncreaseNext5m']=mydata['vwap15s'].shift(-100)/mydata['midPrice']-1
            #mid价格的增长率 1m 2m 5m
            result['midIncreaseNext1m']=mydata['midPrice'].shift(-20)/mydata['midPrice']-1
            result['midIncreaseNext2m']=mydata['midPrice'].shift(-40)/mydata['midPrice']-1
            result['midIncreaseNext5m']=mydata['midPrice'].shift(-100)/mydata['midPrice']-1
            
            #------------------------------------------------------------------
            #剔除14点57分之后，集合竞价的数据
            result=result.iloc[1:]
            result = result.replace(-np.inf, np.nan)
            result = result.replace(np.inf, np.nan)
            result=result[result['tick']<'144700000']
            mycolumns=list(set(result.columns).difference(set(mydata.columns)))
            mycolumns.sort()
            result=result[mycolumns]
            #super().checkDataNan(code,date,self.factor,result)
            pass
        else:
            logger.error(f'There no data of {code} in {date} to computer factor!') 
        return result
########################################################################
